package com.banciyuan.bcywebview.biz.groupdetail;

import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.helper.timelinedatacenter.DetailDataHelper;
import com.banciyuan.bcywebview.base.helper.timelinedatacenter.TimelineLikeHelper;
import com.banciyuan.bcywebview.base.view.toast.MyToast;
import com.banciyuan.bcywebview.biz.pc.zone.smooth.SmoothPersonActivity;
import com.banciyuan.bcywebview.utils.common.UIUtils;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.List;

import de.greenrobot.daoexample.model.DetailComment;
import de.greenrobot.daoexample.model.ForumThread;

/**
 * Created by hy on 2015/7/14.
 */
public class NewTeamDetailAdapter extends BaseAdapter {
    private List<ForumThread> data;
    private Context context;
    private RequestQueue mQueue;
    private ListView mListView;

    public NewTeamDetailAdapter(Context context, List<ForumThread> data) {
        this.context = context;
        this.data = data;
        mQueue = VolleyQueue.getRquest(context);
    }

    @Override
    public int getCount() {
        return data.size() + 1;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == data.size()) {
            if (data.size() == 0) {
                return 4;
            } else {
                return 5;
            }
        } else {
            ForumThread forumThread = data.get(position);
            if (forumThread.getMulti().isEmpty()) {
                return 0;
            } else if (forumThread.getMulti().size() == 1) {
                return 1;
            } else if (forumThread.getMulti().size() == 2) {
                return 2;
            } else {
                return 3;
            }
        }
    }

    @Override
    public int getViewTypeCount() {
        return 6;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {

        ViewTeam viewEmpty = null;
        ViewTeam viewOne = null;
        ViewTeam viewTwo = null;
        ViewTeam viewThree = null;

        if (convertView == null) {
            switch (getItemViewType(position)) {
                case 0:
                    convertView = View.inflate(context, R.layout.talk_detail_item, null);
                    viewEmpty = new ViewTeam(convertView);
                    convertView.setTag(viewEmpty);
                    break;
                case 1:
                    convertView = View.inflate(context, R.layout.talk_detail_item, null);
                    viewOne = new ViewTeam(convertView);
                    convertView.setTag(viewOne);
                    break;
                case 2:
                    convertView = View.inflate(context, R.layout.talk_detail_item, null);
                    viewTwo = new ViewTeam(convertView);
                    convertView.setTag(viewTwo);
                    break;
                case 3:
                    convertView = View.inflate(context, R.layout.talk_detail_item, null);
                    viewThree = new ViewTeam(convertView);
                    convertView.setTag(viewThree);
                    break;
                default:
                    convertView = View.inflate(context, R.layout.talk_detail_item, null);
                    viewEmpty = new ViewTeam(convertView);
                    convertView.setTag(viewEmpty);
                    break;
            }
        } else {
            switch (getItemViewType(position)) {
                case 0:
                    viewEmpty = (ViewTeam) convertView.getTag();
                    break;
                case 1:
                    viewOne = (ViewTeam) convertView.getTag();
                    break;
                case 2:
                    viewTwo = (ViewTeam) convertView.getTag();
                    break;
                case 3:
                    viewThree = (ViewTeam) convertView.getTag();
                    break;
                default:
                    viewEmpty = (ViewTeam) convertView.getTag();
                    break;
            }
        }
        ForumThread mItem = null;
        if (position < data.size()) {
            mItem = data.get(position);
        }
        switch (getItemViewType(position)) {
            case 0:
                setUI(viewEmpty, mItem, position);
                break;
            case 1:
                setUI(viewOne, mItem, position);
                break;
            case 2:
                setUI(viewTwo, mItem, position);
                break;
            case 3:
                setUI(viewThree, mItem, position);
                break;
            case 4:
                convertView = View.inflate(context, R.layout.talk_empty, null);
                break;
            case 5:
                convertView = new View(context);
                AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(-1, UIUtils.dip2px(52, context));
                convertView.setLayoutParams(layoutParams);
                break;
        }
        return convertView;
    }

    private void setUI(ViewTeam viewTeam, ForumThread mItem, int position) {
        if (viewTeam == null) return;
        if (!TextUtils.isEmpty(mItem.getAvatar())) {
            ImageLoader.getInstance().displayImage(mItem.getAvatar(), viewTeam.head_iv, ImageOptions.optionHead);
        }
        if (!TextUtils.isEmpty(mItem.getUname())) {
            viewTeam.user_name.setText(mItem.getUname());
        }
        if (!TextUtils.isEmpty(mItem.getPlain())) {
            viewTeam.content_tv.setVisibility(View.VISIBLE);
            viewTeam.content_tv.setText(Html.fromHtml(mItem.getPlain()));
        } else {
            viewTeam.content_tv.setVisibility(View.GONE);
        }
        if (mItem.getReply_count() > 3) {
            viewTeam.more_comment.setVisibility(View.VISIBLE);
            viewTeam.more_comment.setText(String.format(context.getString(R.string.commnetmore), String.valueOf(mItem.getReply_count())));
        } else {
            viewTeam.more_comment.setVisibility(View.GONE);
        }
        if (!mItem.getMulti().isEmpty()) {
            viewTeam.img_container.setVisibility(View.VISIBLE);
            for (int i = 0; i < mItem.getMulti().size(); i++) {
                if (i < 3) {
                    ImageLoader.getInstance().displayImage(mItem.getMulti().get(i).getPath(), viewTeam.imgs[i], ImageOptions.options);
                }
            }
        } else {
            viewTeam.img_container.setVisibility(View.GONE);
        }
        if (mItem.getReply_data() != null && !mItem.getReply_data().isEmpty()) {
            viewTeam.comment_area.setVisibility(View.VISIBLE);
            int index = 0;
            for (; index < mItem.getReply_data().size(); index++) {
                if (index < 3) {
                    DetailComment commnet = mItem.getReply_data().get(index);
                    String pic_content = "";
                    if (commnet.getMulti() != null && !commnet.getMulti().isEmpty()) {
                        pic_content = context.getString(R.string.img_content_format);
                    }
                    if (commnet.getType() == 1) {
                        viewTeam.commnets[index].setText(Html.fromHtml(String.format(context.getString(R.string.talk_replay_format_type1), commnet.getUname(), commnet.getContent(), pic_content)));
                    } else {
                        viewTeam.commnets[index].setText(Html.fromHtml(String.format(context.getString(R.string.talk_replay_format_type2), commnet.getUname(), commnet.getReplyCotent() + commnet.getContent(), pic_content)));
                    }
                }
            }
            for (int j = 0; j < viewTeam.commnets.length; j++) {
                if (index - 1 >= j) {
                    viewTeam.commnets[j].setVisibility(View.VISIBLE);
                } else {
                    viewTeam.commnets[j].setVisibility(View.GONE);
                }
            }
        } else {
            viewTeam.comment_area.setVisibility(View.GONE);
        }
        if (mItem.getDing_num() == 0) {
            viewTeam.like_tv.setText(context.getString(R.string.good));
        } else {
            viewTeam.like_tv.setText(String.valueOf(mItem.getDing_num()));
        }
        if (mItem.isHave_ding()) {
            viewTeam.like_iv.setImageResource(R.drawable.parised_talk);
        } else {
            viewTeam.like_iv.setImageResource(R.drawable.parise_talk);
        }
        if (mItem.isHave_tuijian()) {
            viewTeam.recomment_iv.setImageResource(R.drawable.detail_recommend_done);
        } else {
            viewTeam.recomment_iv.setImageResource(R.drawable.talk_recommend);
        }
        OnClick onclick = new OnClick(position);
        viewTeam.like_container.setOnClickListener(onclick);
        viewTeam.commnet_iv.setOnClickListener(onclick);
        viewTeam.recomment_iv.setOnClickListener(onclick);
        ((View) (viewTeam.content_tv.getParent())).setOnClickListener(onclick);
        viewTeam.head_iv.setOnClickListener(onclick);
        viewTeam.user_name.setOnClickListener(onclick);
        viewTeam.comment_area.setOnClickListener(onclick);
    }

    public void setListView(ListView mListView) {
        this.mListView = mListView;
    }

    private void updateView(int itemIndex, ForumThread mItem) {
        if (mListView == null) return;
        View convertView = mListView.getChildAt(itemIndex - mListView.getFirstVisiblePosition());
        if (convertView == null) return;
        ViewTeam viewTeam = null;
        if (convertView.getTag() instanceof ViewTeam) viewTeam = (ViewTeam) convertView.getTag();
        else return;
        if (viewTeam == null) return;
        if (mItem.getDing_num() == 0) {
            viewTeam.like_tv.setText(context.getString(R.string.good));
        } else {
            viewTeam.like_tv.setText(String.valueOf(mItem.getDing_num()));
        }
        if (mItem.isHave_ding()) {
            viewTeam.like_iv.setImageResource(R.drawable.parised_talk);
        } else {
            viewTeam.like_iv.setImageResource(R.drawable.parise_talk);
        }
        if (mItem.isHave_tuijian()) {
            viewTeam.recomment_iv.setImageResource(R.drawable.detail_recommend_done);
        } else {
            viewTeam.recomment_iv.setImageResource(R.drawable.talk_recommend);
        }
    }


    public void setData(List<ForumThread> mList) {
        data = mList;
    }

    private class ViewTeam {
        private ImageView head_iv;
        private TextView user_name;
        private TextView content_tv;
        private ImageView[] imgs;
        private TextView[] commnets;
        private TextView more_comment;
        private FrameLayout img_container;
        private ImageView like_iv;
        private ImageView recomment_iv;
        private ImageView commnet_iv;
        private TextView like_tv;
        private LinearLayout like_container;
        private View comment_area;

        public ViewTeam(View convertView) {
            head_iv = (ImageView) convertView.findViewById(R.id.talk_header_iv);
            user_name = (TextView) convertView.findViewById(R.id.talk_user_name);
            content_tv = (TextView) convertView.findViewById(R.id.content_tv);
            imgs = new ImageView[]{
                    (ImageView) convertView.findViewById(R.id.img_one),
                    (ImageView) convertView.findViewById(R.id.img_two),
                    (ImageView) convertView.findViewById(R.id.img_three)};
            commnets = new TextView[]{
                    (TextView) convertView.findViewById(R.id.comment_one),
                    (TextView) convertView.findViewById(R.id.comment_two),
                    (TextView) convertView.findViewById(R.id.comment_three)};
            more_comment = (TextView) convertView.findViewById(R.id.more_comment);
            img_container = (FrameLayout) convertView.findViewById(R.id.ll_pics);
            like_iv = (ImageView) convertView.findViewById(R.id.like_iv);
            recomment_iv = (ImageView) convertView.findViewById(R.id.recommend_iv);
            commnet_iv = (ImageView) convertView.findViewById(R.id.comment_iv);
            like_tv = (TextView) convertView.findViewById(R.id.like_tv);
            like_container = (LinearLayout) convertView.findViewById(R.id.like_line);
            comment_area = convertView.findViewById(R.id.comment_area);
        }
    }

    class OnClick implements android.view.View.OnClickListener {

        int position;
        ForumThread mItem;

        public OnClick(int position) {
            this.position = position;
            this.mItem = data.get(position);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.like_line:
                    doLike(position);
                    break;
                case R.id.comment_area:
                case R.id.comment_iv:
                    DetailDataHelper.gotoGroupDetail(context, mItem.getGid(), mItem.getPost_id(), true);
                    break;
                case R.id.recommend_iv:
                    doRecommend(position);
                    break;
                case R.id.talk_header_iv:
                case R.id.talk_user_name:
                    gotoUtil.gotoAct(context, SmoothPersonActivity.class, mItem.getUid());
                    break;
                case R.id.talk_card:
                    DetailDataHelper.gotoGroupDetail(context, mItem.getGid(), mItem.getPost_id(), false);
                    break;
                default:
                    break;
            }
        }
    }

    private void doLike(final int position) {
        ForumThread forumThread = data.get(position);
        TimelineLikeHelper.doLikeGroup(context, mQueue, forumThread.getPost_id(), String.valueOf(forumThread.isHave_ding()), new TimelineLikeHelper.TimelineSuccess() {
            @Override
            public void onTimelineSuccess() {
                if (!data.get(position).isHave_ding()) {
                    data.get(position).setHave_ding(true);
                    data.get(position).setDing_num(data.get(position).getDing_num() + 1);
                } else {
                    data.get(position).setHave_ding(false);
                    data.get(position).setDing_num(data.get(position).getDing_num() - 1);
                }
                updateView(position + 2, data.get(position));
            }
        });
    }


    private void doRecommend(final int position) {
        ForumThread forumThread = data.get(position);
        TimelineLikeHelper.doRecommendGroup(context, mQueue, forumThread.getPost_id(), String.valueOf(forumThread.isHave_tuijian()), new TimelineLikeHelper.TimelineSuccess() {
            @Override
            public void onTimelineSuccess() {
                data.get(position).setHave_tuijian(!data.get(position).isHave_tuijian());
                updateView(position + 2, data.get(position));
                if (data.get(position).isHave_tuijian()) {
                    MyToast.show(context, context.getString(R.string.recommend_succ));
                } else {
                    MyToast.show(context, context.getString(R.string.unrecommend_succ));
                }
            }
        });
    }
}
